package com.dingding.service.mall.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import com.dingding.common.core.base.dao.impl.BaseDaoImpl;
import com.dingding.common.core.beans.Commodity;
import com.dingding.facade.mall.dto.MallItem;
import com.dingding.service.mall.dao.ICommodityDao;

/**
 * @Description: 商品表dao
 * @author fengshuonan
 * @date 2016年3月23日 下午4:26:55
 */
@Component("commodityDao")
public class CommodityDaoImpl extends BaseDaoImpl<Commodity> implements ICommodityDao {

	private Logger log = Logger.getLogger(this.getClass());

	/**
	 * @Description: 获取商城商品列表
	 * @author fengshuonan
	 * @return List<MallItem>
	 */
	public List<MallItem> getMallItems() {

		List<Commodity> commodities = null;

		Criterion criterion = Restrictions.and(
				Restrictions.eq("commodityStatus", 1),Restrictions.or(
						Restrictions.eq("commodityStockLimit", 2),Restrictions.and(
								Restrictions.eq("commodityStockLimit", 1), Restrictions.gt("commodityStock", 0))));

		try {
			commodities = super.queryCriteriaList(criterion);
		} catch (Exception e) {
			log.error("获取商品列表出错！CommodityDaoImpl：getMallItems（）", e);
		}

		if (commodities == null || commodities.size() <= 0) {
			return null;
		}

		List<MallItem> list = new ArrayList<MallItem>();

		for (Commodity c : commodities) {
			MallItem mall = new MallItem();
			BeanUtils.copyProperties(c, mall);
			list.add(mall);
		}

		return list;
	}

	/**
	 * @Description: 获取商品价格
	 * @param itemId
	 *            商品id
	 * @author fengshuonan
	 * @return String 商品价格
	 * @Override
	 */
	public double getItemPriceById(String itemId) {

		Commodity commodity = null;

		try {
			commodity = super.getById(itemId);
		} catch (Exception e) {
			log.error("获取商品出现异常！CommodityDaoImpl：getItemPriceById（）", e);
		}

		if (commodity != null) {
			return commodity.getCommodityPrice();
		}

		return 0;
	}

	/**
	 * @Description: 商品是否有库存
	 * @param itemId
	 *            商品id
	 * @author fengshuonan
	 * @return true 有库存 false没库存
	 */
	public synchronized boolean getItemStockStatus(String itemId) {

		Commodity commodity = null;

		try {
			commodity = super.getById(itemId);
		} catch (Exception e) {
			log.error("获取商品出现异常！CommodityDaoImpl：getItemStock（）", e);
		}

		if (commodity != null) {
			if (commodity.getCommodityStockLimit() == 2) {
				return true;
			} else if (commodity.getCommodityStockLimit() == 1) {
				if (commodity.getCommodityStock() != null && commodity.getCommodityStock() > 0) {
					return true;
				}
			}
		}

		return false;
	}

	/**
	 * @Description: 修改商品数量
	 * @param itemId
	 *            商品id
	 * @param nums
	 *            正数为增加，负数为减少商品数量
	 * @author fengshuonan
	 * @return void
	 */
	public synchronized void updateItemNums(String itemId, int nums) {

		Commodity commodity = null;

		try {
			commodity = super.getById(itemId);
		} catch (Exception e) {
			log.error("获取商品出现异常！CommodityDaoImpl：getItemStock（）", e);
		}

		if (commodity != null && commodity.getCommodityStockLimit() == 1) {
			if (commodity.getCommodityStock() + nums >= 0) {
				commodity.setCommodityStock(commodity.getCommodityStock() + nums);
			}
		}
	}

	/**
	 * @Title: getItemTypeById
	 * @Description:根据id获取商品类型
	 * @param @param
	 *            itemId
	 * @param @return
	 *            商品类型
	 * @author zhouxuan
	 */
	public int getItemTypeById(String itemId) {
		Commodity commodity = null;

		try {
			commodity = super.getById(itemId);
		} catch (Exception e) {
			log.error("获取商品类型出现异常！CommodityDaoImpl：getItemTypeById（）", e);
		}

		if (commodity != null) {
			return commodity.getCommodityType();
		}

		return 0;
	}

	/**
	 * @Title: getExpiredDate
	 * @Description:获取商品有效期
	 * @param @param
	 *            itemId
	 * @author zhouxuan
	 * @return int
	 */

	public int getExpiredDate(String itemId) {
		Commodity commodity = null;

		try {
			commodity = super.getById(itemId);
		} catch (Exception e) {
			log.error("获取商品有效期出现异常！CommodityDaoImpl：getExpiredDate（）", e);
		}

		return commodity.getExpiredDate();
	}

}
